﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MovingChame.Web.Models;
using MovingChame.Web.Infrastructure;

namespace MovingChame.Web.Controllers
{
    public class LocationController : Controller
    {
        private ILocationProvider locationProvider = LocationProvider.Current;

        //
        // GET: /Location/

        public ActionResult Index()
        {
            return View(locationProvider.GetAllLocations());
        }

        //
        // GET: /Location/Details/5

        public ActionResult Details(int id = 0)
        {
            Location location = locationProvider.GetLocationById(id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        //
        // GET: /Location/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Location/Create

        [HttpPost]
        public ActionResult Create(Location location)
        {
            if (ModelState.IsValid)
            {
                locationProvider.AddLocation(location.Name, location.Center.Longitude.Value, location.Center.Latitude.Value, location.ImagePath, location.LongDescription);
                return RedirectToAction("Index");
            }

            return View(location);
        }

        //
        // GET: /Location/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Location location = locationProvider.GetLocationById(id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        //
        // POST: /Location/Edit/5

        [HttpPost]
        public ActionResult Edit(Location location)
        {
            if (ModelState.IsValid)
            {
                locationProvider.UpdateLocation(location.Id, location);
                return RedirectToAction("Index");
            }
            return View(location);
        }

        //
        // GET: /Location/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Location location = locationProvider.GetLocationById(id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        //
        // POST: /Location/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            locationProvider.RemoveLocation(id);
            return RedirectToAction("Index");
        }

        [HttpPost]
        public string SearchIndex(FormCollection fc, string searchString)
        {
            return "<h3> From [HttpPost]SearchIndex: " + searchString + "</h3>";
        }

        public ActionResult SearchIndex(string searchString)
        {
            List<Location> locations = new List<Location>();
            if (!String.IsNullOrEmpty(searchString))
            {
                locations = locationProvider.SearchLocationInName(searchString).ToList();
            }
            return View(locations);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}